<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/hamming-distance/
      思路：https://juejin.im/post/6844904175562653710#heading-70
      https://leetcode-cn.com/problems/hamming-distance/solution/javascriptwei-yun-suan-jie-fa-by-zhu-zhu-xia-6/
     */
    let x = 1, y = 4;

    // 1.https://juejin.im/post/6844904175562653710#heading-70
    function hammingDistance(x, y) {
      let res = x ^ y;
      let count = 0;

      while (res) {
        if (res & 1)
          count++;

        res >>= 1;
      }
      return count;
    }

    // 2.https://leetcode-cn.com/problems/hamming-distance/solution/javascriptwei-yun-suan-jie-fa-by-zhu-zhu-xia-6/
    // function hammingDistance(x, y) {
    //   let res = 0;
    //   while (x !== 0 || y !== 0) {
    //     if ((x & 1) !== (y & 1)) 
    //       res++;

    //     x >>= 1;
    //     y >>= 1;
    //   }
    //   return res;
    // }
    console.log(hammingDistance(x, y));
  </script>
</body>
</html>